package util

import (
	"Graphite/database"
	"Graphite/model"
	"context"
	"fmt"
	"github.com/gin-gonic/gin"
	"go.mongodb.org/mongo-driver/bson"
	"go.mongodb.org/mongo-driver/bson/primitive"
	"log"
)

type Res struct {
	Code int
	Msg  string
	Data interface{}
}

func Success(res interface{}) Res {
	fmt.Println(res)
	return Res{
		Code: SUCCESS,
		Msg:  GetMsg(SUCCESS),
		Data: res,
	}
}
func Fail(errCode int, err error) Res {
	return Res{
		Code: errCode,
		Msg:  GetMsg(errCode),
		Data: err.Error(),
	}
}

func Return(c *gin.Context, HttpCode int, code int, data interface{}) {
	c.JSON(HttpCode, gin.H{
		"code": code,
		"msg":  GetMsg(code),
		"data": data,
	})
}

//货物管理获取信息用
func Return2(c *gin.Context,HttpCode int, code int, data interface{},data2 interface{}) {
	c.JSON(HttpCode, gin.H{
		"code": code,
		"msg": GetMsg(code),
		"results": data,
		"results2":data2,
	})
}

func Result(id primitive.ObjectID, col string) interface{} {
	db := database.GetMongoDB()
	collection := db.Collection(col)
	var one interface{}
	err := collection.FindOne(context.Background(), bson.M{"_id": id}).Decode(&one)
	if err != nil {
		log.Println(err)
	}
	return one
}
func ResultAmount(id primitive.ObjectID, col string) interface{} {
	db := database.GetMongoDB()
	collection := db.Collection(col)
	var one model.Amount
	err := collection.FindOne(context.Background(), bson.M{"_id": id}).Decode(&one)
	if err != nil {
		log.Println(err)
	}
	return one
}